package com.cat.model.web.action.admin.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.AjaxObject;
import com.cat.model.biz.logic.SystemLogic;
import com.cat.model.biz.service.admin.SystemService;
import com.cat.model.common.model.admin.AdminModuleVO;
import com.cat.model.common.model.admin.AdminRolePermissionVO;
import com.cat.model.common.model.admin.AdminRoleVO;
import com.cat.model.web.action.base.AdminAction;

public class RolePermissionManagerAction extends AdminAction{

	@Autowired
	private SystemService systemService;
	
	@Autowired
	private SystemLogic systemLogic;
	
	private static final long serialVersionUID = 425636316597986569L;
	
	private AdminRoleVO adminRole;
	private Long id;
	private String roleId;
	private String permission;
	
	public String perlist() {
		
		return "perlist";
	}
	
	public String insertRolePermission(){
		try { 
			AdminRolePermissionVO rolePermission = new AdminRolePermissionVO();
			rolePermission.setRoleId(new Long(roleId));
			rolePermission.setPermission(permission);
			systemService.insertAdminRolePermission(rolePermission);
		} catch (Exception e) {
			e.printStackTrace();
			this.print(AjaxObject.newError(e.getMessage()).setCallbackType("").toString());
			return "perlist";
		}
		this.print(AjaxObject.newOk("保存成功！").setCallbackType("").toString());
		return "perlist";
	}
	
	public void doModuleTree() throws IOException{
		List<AdminModuleVO> list = systemLogic.getAllAdminModule(null);
		List<String> lstTree = new ArrayList<String>();
		String s1 = "{id:0, pId:0, name:\"菜单管理\" , open:true,nocheck:true}"; 
		lstTree.add(s1); 
		String[] perssions = null; 
		if(roleId != null){
			AdminRolePermissionVO perssion = systemLogic.getAdminRolePermission(new Long(roleId));
			if(perssion!=null){
				perssions = perssion.getPermission().split(",");
			}
		}
		
		if(list!=null && list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				AdminModuleVO module = (AdminModuleVO)list.get(i);
				String s = "{id:\""+module.getId()+"\", pId:\""+module.getParentId()+"\", name:\""+module.getName()+"\" }";
				if(perssions != null){
					for (int j = 0; j < perssions.length; j++) {
						String moduleId = perssions[j];
						if(moduleId.equals(module.getId().toString())){
							s = "{id:\""+module.getId()+"\", pId:\""+module.getParentId()+"\", name:\""+module.getName()+"\" ,checked :true}";
							break;
						}
					}
				}
				 lstTree.add(s);
			}
		} 
        response.getWriter().print(net.sf.json.JSONArray .fromObject(lstTree).toString());  
    }

	public AdminRoleVO getAdminRole() {
		return adminRole;
	}

	public void setAdminRole(AdminRoleVO adminRole) {
		this.adminRole = adminRole;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

}
